from django.contrib.auth import authenticate, login
from django.http import HttpResponse
from django.shortcuts import render, redirect


def query(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        username = request.POST.get('username')
        password = request.POST.get('password')
        user_date = {'username': username, 'password': password}
        user_obj = authenticate(request, **user_date)  # 验证用户名与密码是否正确，可以自定义 backend
        if not user_obj:
            return HttpResponse('用户名或者密码错误！')
        else:
            login(request, user_obj)  # 登录，将当前用户信息存储在会话中
            return redirect('index')
